<?php


namespace app\admin\controller;


use app\common\model\UserActive;
use Carbon\Carbon;
use think\Db;

class Analysis extends Base
{


    public function index()
    {
        $where = [];

        if(!isEmpty($_GET['type'])){
            $where['type'] = trim(input('get.type'));
        }

        if(!empty($_GET['startDate'])&&empty($_GET['endDate'])){
            $where['date'] = ['>= time',strtotime($_GET['startDate'])];
        }elseif(!empty($_GET['endDate'])&&empty($_GET['startDate'])){
            $where['date'] = ['<= time',strtotime($_GET['endDate'])];
        }elseif(!empty($_GET['startDate'])&&!empty($_GET['endDate'])){
            $where['date'] = ['between time',array(strtotime($_GET['startDate']),strtotime($_GET['endDate']))];
        }


        $model = new UserActive();
        $rows = $model->getList($where);
        $data = Db::name('banner_cate')->where('status',1)->select();
        $this->assign([
            'title' => '活跃用户',
            'data' => $data,
            'pageHTML' => $rows->render(),
            'rows' => $rows
        ]);
        return view();
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 统计图表
     */
    public function map(){

        $map = [];

        if(!isEmpty($_GET['type'])){
            $map['type'] = trim(input('get.type'));
        }

        $map['type'] = 2;

        $field = "DATE(date) AS dates ,SUM(man) as man,SUM(woman) as woman,user_total,COUNT(id) as count";

        //是否按小时分析
        $is_hour = false;
        if(!empty($_GET['startDate'])&&empty($_GET['endDate'])){
            $map['date'] = ['>= time',strtotime($_GET['startDate'])];
        }elseif(!empty($_GET['endDate'])&&empty($_GET['startDate'])){
            $map['date'] = ['<= time',strtotime($_GET['endDate'])];
        }elseif(!empty($_GET['startDate'])&&!empty($_GET['endDate'])){

            if ($_GET['startDate'] == $_GET['endDate']){
                $is_hour = true;
                $map['type'] = 1;
                $field = "HOUR(date) AS dates ,SUM(man) as man,SUM(woman) as woman,SUM(user_total) as user_total,COUNT(id) as count";
                $map['date'] = ['between time',array(strtotime($_GET['startDate']),strtotime($_GET['endDate'].' 23:59:59'))];
            }else{
                $map['date'] = ['between time',array(strtotime($_GET['startDate']),strtotime($_GET['endDate'].' 23:59:59'))];
            }

        } else{
            $map['type'] = 1;
            $map['date'] = ['between time',[Carbon::now()->startOfDay()->timestamp,Carbon::now()->endOfDay()->timestamp]];
            $field = "HOUR(date) AS dates ,SUM(man) as man,SUM(woman) as woman,SUM(user_total) as user_total,COUNT(id) as count";
        }


        $res  = Db::name('user_active')->where($map)->fieldRaw($field)->group('dates')->order('dates')->select();

        foreach ($res as $k => $v){
            if ($v['count'] != 1){
                $res[$k]['man']  = bcdiv($v['man'],$v['count'],0);
                $res[$k]['woman']  = bcdiv($v['woman'],$v['count'],0);
                $res[$k]['user_total']  = bcdiv($v['user_total'],$v['count'],0);
            }

            //总活跃人数
            $res[$k]['people'] = $res[$k]['man'] + $res[$k]['woman'];
            //男用户活跃度
            $res[$k]['man_ratio'] = number_format(bcdiv($res[$k]['man'],$res[$k]['user_total'],4) * 100,2);
            //女用户活跃度
            $res[$k]['woman_ratio'] = number_format(bcdiv($res[$k]['woman'],$res[$k]['user_total'],4) * 100,2);
            //用户活跃度
            $res[$k]['ratio'] = number_format(bcdiv(($res[$k]['man']+$res[$k]['woman']),$res[$k]['user_total'],4) * 100,2);

        }

        $this->assign([
            'res'=>$res,
            'is_hour' => $is_hour,
            'days' => json_encode(array_column($res,'dates')),
            'man' => json_encode(array_column($res,'man')),
            'woman' => json_encode(array_column($res,'woman')),
            'people' => json_encode(array_column($res,'people')),
            'man_ratio' => json_encode(array_column($res,'man_ratio')),
            'woman_ratio' => json_encode(array_column($res,'woman_ratio')),
            'ratio' => json_encode(array_column($res,'ratio')),
        ]);
        return view();
    }

}